/**
 * 
 */
package optimization.naturalOptimization.evolutionaryAlgorithm.evolutionaryOperator.selection;

import optimization.naturalOptimization.evolutionaryAlgorithm.EvolutionaryAlgorithm;
import optimization.naturalOptimization.evolutionaryAlgorithm.evolutionaryOperator.EvolutionaryOperator;
import optimization.naturalOptimization.population.individual.Individual;

/**
 * This class provides the superclass for all selectors.
 * @author Kevin Wagner
 * @version 1.0
 */
public abstract class Selector extends EvolutionaryOperator {

	/**
	 * Creates a new selector.
	 * @param parentEA
	 */
	public Selector(EvolutionaryAlgorithm parentEA) {
		super(parentEA);
	}
	
	/**
	 * Sorts the individuals by fitness value from best to worst.
	 * 
	 * @param individuals
	 */
	@SuppressWarnings("rawtypes")
	protected void sortIndividuals(Individual[] individuals) {
		boolean unsortiert = true;
		Individual temp;

		while (unsortiert) {
			unsortiert = false;
			for (int i = 0; i < individuals.length - 1; i++) {
				Individual ind1 = individuals[i];

				Individual ind2 = individuals[i + 1];
				if (getParentEA().getFitness().compare(
						ind1.getPositionFitness(), ind2.getPositionFitness()) == 2) {
					temp = individuals[i];
					individuals[i] = individuals[i + 1];
					individuals[i + 1] = temp;
					unsortiert = true;
				}
			}
		}
	}

}
